package map;
/*
Map的三种遍历方法
 */
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapDemo03 {
    public static void main(String[] args) {
        HashMap<String,String> hm = new HashMap<>();
        hm.put("张三","北京");
        hm.put("李四","美国");
        hm.put("王五","北京");

        method3(hm);
    }

    private static void method3(HashMap<String, String> hm) {
        //调用forEash方法，给一个匿名内部类，重写方法
        hm.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String key, String value) {
                System.out.println(key+":"+value);
            }
        });
        //lambda表达式
        hm.forEach((key, value) -> System.out.println(key+":"+value));
    }

    private static void method2(HashMap<String, String> hm) {
        //1.获取所有的键值对对象，Entry是Map里的接口
       Set<Map.Entry<String,String>> entrySet = hm.entrySet();
       for(Map.Entry<String,String> entry : entrySet) {
           System.out.println(entry.getKey()+":"+entry.getValue());
       }
    }

    private static void method1(HashMap<String, String> hm) {
        //1.获取所有的键
        Set<String> keySet = hm.keySet();
        //2.遍历set集合，获取每一个键
        for (String key : keySet) {
            //3.调用map的get方法，获取值
            System.out.println(key+"--"+ hm.get(key));
        }
    }
}
